<?php 
// // $stay_processing_data = '{"version":1,"sn":"211650552","type":1001,"firmware_version":"3.1.4.1","upload_timestamp":1521021450717,"data":{"screen_on_off_timestamp":[[1521021343998,1521021345486],[1521021430290,1521021433309],[1521021449482,1521021450717]]}}';
// // echo "<pre>";
// // $stay_processing_data = '{
// //      "version":1,
// //      "sn":"H123456778",
// //      "type":1002,
// //      "firmware_version":"3.1.1.1",
// //      "upload_timestamp":1111112312,
// //      "data":{
// //          "install":[
// //               {
// //                   "app_name":"嗨镜",
// //                   "package_name":"com.example.project",
// //                   "timestamp":12345678
// //               },
// //               {
// //                   "app_name":"嗨镜1",
// //                   "package_name":"com.example.project1",
// //                   "timestamp":12345678
// //               }
// //          ]
// //      }
// //  }';
// // $stay_processing_data = '{
// //      "version":1,
// //      "sn":"H123456778",
// //      "type":1002,
// //      "firmware_version":"3.1.1.1",
// //      "upload_timestamp":1111112312,
// //      "data":{
// //          "uninstall":[
// //               {
// //                   "app_name":"嗨镜",
// //                   "package_name":"com.example.project",
// //                   "timestamp":12345678
// //               },
// //               {
// //                   "app_name":"嗨镜1",
// //                   "package_name":"com.example.project1",
// //                   "timestamp":12345678
// //               }
// //          ]
// //      }
// //  }';
// $stay_processing_data = '{
//       "version":1,
//       "sn":"H123456778",
//       "type":1000,
//       "firmware_version":"3.1.1.1",
//       "upload_timestamp":1111112312,
//       "data":[
//           {
//               "app_name":"aiqiyi",
//               "package_name":"hispot.aiqiyi.com",
//               "open_close_timestamp":[
//                   [1521021343998,1521021345486]
//               ]
//           }
//       ]
//  }';
header("content-type:text/html;charset=utf8");
$stay_processing_data = $_POST['data'];
$data = json_decode($stay_processing_data,true);
$pdo = new PDO("mysql:host=123.57.16.97;dbname=statistics","root","zqVnUy6cxsQXbpEZ");
$pdo->exec("set names utf8");
if ($data['type'] == '1000') {
	$res = application_day_active($data,$pdo);
	$res1 = application_time_interval($data,$pdo);
	$res2 = application_use_duration($data,$pdo);
	if ($res && $res1 && $res2) {
		echo "OK";
	}else{
		echo "NO";
	}
}elseif($data['type'] == '1001'){
    $res = device_use_duration($data,$pdo);
	$res1 = device_info($data,$pdo);
	$res2 = device_time_interval($data,$pdo);
	$res3 = device_day_active($data,$pdo);
	if ($res && $res1 && $res2 && $res3) {
		echo "OK";
	}else{
		echo "NO";
	}
}elseif($data['type'] == '1002'){
	$res = application_info($data,$pdo);
	if ($res) {
		echo "OK";
	}else{
		echo "NO";
	}
}
//设备日均时段在线统计
function device_time_interval($data,$pdo){
	if (!empty($data['data']['boot_shutdown_timestamp'])) {
		foreach ($data['data']['boot_shutdown_timestamp'] as $key => $val) {
			$start_time = date("H",$val[0]);
			$end_time = date("H",$val[1]);
			$current_date = date("Y-m-d H",$val[0]).":00:00";
			$head = substr($current_date,0,11);
			$tail = substr($current_date,13);
			for ($i=$start_time; $i <= $end_time; $i++) { 
				$whole_point = strtotime($head."$i".$tail);
				$select_sql = "SELECT * FROM statistics_device_time_interval WHERE `time` = '$whole_point'";
				$select_data = $pdo->query($select_sql)->fetch(PDO::FETCH_ASSOC);
				if ($select_data) {
					$number = $select_data['number']+1;
					$sql = "UPDATE statistics_device_time_interval SET `number` = '$number' WHERE `time` = '$whole_point'";
				}else{
					$sql = "INSERT INTO statistics_device_time_interval(`time`,`number`) VALUES('$whole_point','1')";
				}
				$res = $pdo->exec($sql);
				if ($res) {
					return true;
				}else{
					return false;
				}
			}
		}
	}else{
		return true;
	}
}

//应用日均时段在线统计 
function application_time_interval($data,$pdo){
	if (!empty($data['data'])) {
		foreach($data['data'] as $key => $value) {
			$application_name = $value['app_name'];
			$package_name = $value['package_name'];
			if (!empty($value['open_close_timestamp'])) {
				foreach ($value['open_close_timestamp'] as $key => $val) {
					$start_time = date("H",$val[0]);
					$end_time = date("H",$val[1]);
					$current_date = date("Y-m-d H",$val[0]).":00:00";
					$head = substr($current_date,0,11);
					$tail = substr($current_date,13);
					for ($i=$start_time; $i <= $end_time; $i++) { 
						$whole_point = strtotime($head."$i".$tail);
						$select_sql = "SELECT * FROM statistics_application_time_interval WHERE application_name = '$application_name' AND package_name = '$package_name' AND `time` = '$whole_point'";
						$select_data = $pdo->query($select_sql)->fetch(PDO::FETCH_ASSOC);
						if ($select_data) {
							$number = $select_data['number']+1;
							$sql = "UPDATE statistics_application_time_interval SET `number` = '$number' WHERE application_name = '$application_name' AND package_name = '$package_name' AND `time` = '$whole_point'";
						}else{
							$sql = "INSERT INTO statistics_application_time_interval(application_name,package_name,`time`,`number`) VALUES('$application_name','$package_name','$whole_point','1')";
						}
						$res = $pdo->exec($sql);
						if ($res) {
							return true;
						}else{
							return false;
						}
					}
				}
			}else{
				return true;
			}
		}
	}else{
		return true;
	}
}

//应用信息统计
function application_info($data,$pdo){
	$return = true;
	if (!empty($data['data']['install'])) {
		$install = array();
		foreach ($data['data']['install'] as $key => $val) {
			$install['app_name'][] = $val['app_name'];	
			$install['package_name'][] = $val['package_name'];	
			$install['time'][] = $val['timestamp'];
			$install['install'][] = 1;	
		}
		for ($i=0; $i < count($install['app_name']); $i++) { 
			$application_name = $install['app_name'][$i];
			$package_name = $install['package_name'][$i];
			$install_amount = $install['install'][$i];
			$time = $install['time'][$i];
			$sql = "INSERT INTO statistics_application_info(application_name,package_name,install,`time`) VALUES('$application_name','$package_name','$install_amount','$time')";
			$res = $pdo->exec($sql);
			if (!$res) {
				$return = false;
			}
		}
	}
	if (!empty($data['data']['uninstall'])) {
		$uninstall = array();
		foreach ($data['data']['uninstall'] as $key => $val) {
			$uninstall['app_name'][] = $val['app_name'];	
			$uninstall['package_name'][] = $val['package_name'];	
			$uninstall['time'][] = $val['timestamp'];
			$uninstall['uninstall'][] = 1;	
		}	
		for ($i=0; $i < count($uninstall['app_name']); $i++) { 
			$application_name = $uninstall['app_name'][$i];
			$package_name = $uninstall['package_name'][$i];
			$uninstall_amount = $uninstall['uninstall'][$i];
			$time = $uninstall['time'][$i];
			$sql = "INSERT INTO statistics_application_info(application_name,package_name,uninstall,`time`) VALUES('$application_name','$package_name','$uninstall_amount','$time')";
			$res = $pdo->exec($sql);
			if (!$res) {
				$return = false;
			}
		}
	}
	return $return;
}

//设备信息统计
function device_info($data,$pdo){
	$sn = $data['sn'];
	$last_login_time = strtotime(date("Y-m-d H:i:s",$data['upload_timestamp']));
	$system_version = $data['firmware_version'];
	$pdo_device_binding = new PDO("mysql:host=123.57.16.97;dbname=device_binding","root","zqVnUy6cxsQXbpEZ");
	$pdo_device_binding->exec("set names utf8");
	$select_uid_sql = "SELECT u_id FROM statistics WHERE device_sn = '$sn'";
	$device_binding_uid = $pdo_device_binding->query($select_uid_sql)->fetch(PDO::FETCH_ASSOC);
	if ($device_binding_uid) {
		$select_tel_sql = "SELECT tel FROM user WHERE u_id = '".$device_binding_uid['u_id']."'";
		$tel_data = $pdo_device_binding->query($select_tel_sql)->fetch(PDO::FETCH_ASSOC);
		$tel = $tel_data['tel'];
	}else{
		$tel = '';
	}
	$select_sql = "SELECT * FROM statistics_device_info WHERE device_sn = '$sn'";
	$select_data = $pdo->query($select_sql)->fetch(PDO::FETCH_ASSOC);
	if ($select_data) {
		$sql = "UPDATE statistics_device_info SET last_login_time = '$last_login_time',binding_tel = '$tel',system_version = '$system_version' WHERE device_sn = '$sn'";
	}else{
		$sql = "INSERT INTO statistics_device_info(device_sn,last_login_time,binding_tel,system_version) VALUES('$sn','$last_login_time','$tel','$system_version')";
	}
	$res = $pdo->exec($sql);
	if ($res) {
		return true;
	}else{
		return false;
	}
}

//设备使用时长统计
function device_use_duration($data,$pdo){
	if (!empty($data['data']['boot_shutdown_timestamp'])) {
		$use_time = 0;
		foreach ($data['data']['boot_shutdown_timestamp'] as $key => $value) {
			$use_time += $value[1]-$value[0];
		}
		$use_time = ceil($use_time/60);
		$sn = $data['sn'];
		$time = strtotime(date("Y-m-d",$data['upload_timestamp']));
		$select_sql = "SELECT * FROM statistics_device_use_duration WHERE device_sn = '$sn' AND `time` = '$time'";
		$select_data = $pdo->query($select_sql)->fetch(PDO::FETCH_ASSOC);
		if ($select_data) {
			$use_duration = $select_data['use_duration']+$use_time;
			$sql = "UPDATE statistics_device_use_duration SET use_duration = '$use_duration' WHERE device_sn = '$sn' AND `time` = '$time'";
		}else{
			$sql = "INSERT INTO statistics_device_use_duration(device_sn,`time`,use_duration) VALUES('$sn','$time','$use_time')";
		}
		$res = $pdo->exec($sql);
		if ($res) {
			return true;
		}else{
			return false;
		}
	}else{
		return true;
	}
}

//应用使用时长统计
function application_use_duration($data,$pdo){
	if (!empty($data['data'])) {
		foreach ($data['data'] as $key => $value) {
			$app_name = $value['app_name'];
			$package_name = $value['package_name'];
			$use_time = 0;
			if (!empty($value['open_close_timestamp'])) {
				foreach ($value['open_close_timestamp'] as $key => $val) {
					$use_time += $val[1]-$val[0];
				}
				$use_time = ceil($use_time/60);
				$time = strtotime(date("Y-m-d",$data['upload_timestamp']));
				$select_sql = "SELECT * FROM statistics_application_use_duration WHERE application_name = '$app_name' AND package_name = '$package_name' AND `time` = '$time'";
				$select_data = $pdo->query($select_sql)->fetch(PDO::FETCH_ASSOC);
				if ($select_data) {
					$use_duration = $select_data['use_duration']+$use_time;
					$sql = "UPDATE statistics_application_use_duration SET use_duration = '$use_duration' WHERE application_name = '$app_name' AND package_name = '$package_name' AND `time` = '$time'";
				}else{
					$sql = "INSERT INTO statistics_application_use_duration(application_name,package_name,`time`,use_duration) VALUES('$app_name','$package_name','$time','$use_time')";
				}
				$res = $pdo->exec($sql);
				if ($res) {
					return true;
				}else{
					return false;
				}	
			}else{
				true;
			}
		}
	}else{
		return true;
	}
}

//设备日活跃统计
function device_day_active($data,$pdo){
	if (!empty($data['data']['boot_shutdown_timestamp'])) {
		$arr = array();
		foreach ($data['data']['boot_shutdown_timestamp'] as $key => $val) {
			$arr[date("Y-m-d",$val[0])] = date("Y-m-d",$val[0]);
			$arr[date("Y-m-d",$val[1])] = date("Y-m-d",$val[1]);
		}
		foreach ($arr as $key => $value) {
			$time = strtotime($value);
			$select_sql = "SELECT * FROM statistics_device_day_active WHERE `time` = '$time'";;
			$select_data = $pdo->query($select_sql)->fetch(PDO::FETCH_ASSOC);
			if ($select_data) {
				$number = $select_data['number']+1;
				$sql = "UPDATE statistics_device_day_active SET `number` = '$number' WHERE `time` = '$time'";
			}else{
				$sql = "INSERT INTO statistics_device_day_active(`time`,`number`) VALUES('$time','1')";
			}
			$pdo->exec($sql);$res = $pdo->exec($sql);
			if ($res) {
				return true;
			}else{
				return false;
			}
		}
	}else{
		return true;
	}
}

//应用日活跃统计
function application_day_active($data,$pdo){
	if (!empty($data['data'])) {
		$arr = array();
		$app_name = '';
		$package_name = '';
		foreach($data["data"] as $key => $value) {
			$app_name = $value['app_name'];
			$package_name = $value['package_name'];
			foreach ($value['open_close_timestamp'] as $key => $val) {
				$arr[date("Y-m-d",$val[0])] = date("Y-m-d",$val[0]);
				$arr[date("Y-m-d",$val[1])] = date("Y-m-d",$val[1]);
			}
			foreach ($arr as $key => $value) {
				$time = strtotime($value);
				$select_sql = "SELECT * FROM statistics_application_day_active WHERE application_name = '$app_name' AND package_name = '$package_name' AND `time` = '$time'";
				$select_data = $pdo->query($select_sql)->fetch(PDO::FETCH_ASSOC);
				if ($select_data) {
					$number = $select_data['number']+1;
					$sql = "UPDATE statistics_application_day_active SET `number` = '$number' WHERE application_name = '$app_name' AND package_name = '$package_name' AND `time` = '$time'";
				}else{
					$sql = "INSERT INTO statistics_application_day_active(application_name,package_name,`time`,`number`) VALUES('$app_name','$package_name','$time','1')";
				}
				$res = $pdo->exec($sql);
				if ($res) {
					return true;
				}else{
					return false;
				}
			}
		}
	}else{
		return true;
	}
}

 ?>